using System;
using System.Collections.Generic;
using System.Text;

namespace HandyIRCCore
{
    public class HandyIRCMessage
    {
        #region Declarations

        private string rawMessage = "";

        private string prefix = "";

        private string command = "";

        private List<string> parameters = new List<string>();

        private string nick = "";

        private string ident = "";

        private string host = "";

        private int currentCharInd = 0;

        #endregion

        #region Methods

        public HandyIRCMessage(string message)
        {
            rawMessage = message;

            string token = GetNextToken(false);

            // is prefix exists?
            if (token[0] == ':')
            {
                prefix = token.Substring(1);
                ParsePrefix();

                token = GetNextToken(false);
            }

            command = token;

            // parsing parameters
            token = GetNextToken(true);
            while (token.Length > 0)
            {
                parameters.Add(token);
                token = GetNextToken(true);
            }
        }

        private string GetNextToken(bool withTrailingCheck)
        {
            string result = "";

            if (currentCharInd < rawMessage.Length)
            {
                int nextCharInd = rawMessage.IndexOf(' ', currentCharInd);
                if (nextCharInd == -1)
                    nextCharInd = rawMessage.Length;

                if (withTrailingCheck && rawMessage[currentCharInd] == ':')
                {
                    result = rawMessage.Substring(currentCharInd + 1);
                    nextCharInd = rawMessage.Length;
                }
                else
                    result = rawMessage.Substring(currentCharInd, nextCharInd - currentCharInd);

                currentCharInd = nextCharInd + 1;
            }

            return result;
        }

        private void ParsePrefix()
        {
            int begin = 0;
            int end = prefix.IndexOf('!');

            if (end != -1)
            {
                nick = prefix.Substring(begin, end - begin);

                begin = end + 1;
                end = prefix.IndexOf('@');

                ident = prefix.Substring(begin, end - begin);
                host = prefix.Substring(end + 1);
            }
            else
            {
                end = prefix.IndexOf('@');

                if (end != -1)
                {
                    nick = prefix.Substring(begin, end - begin);
                    host = prefix.Substring(end + 1);
                }
            }
        }

        #endregion

        #region Properties

        public string RawMessage
        {
            get
            {
                return rawMessage;
            }
        }

        public string Prefix
        {
            get
            {
                return prefix;
            }
        }

        public string Command
        {
            get
            {
                return command;
            }
        }

        public List<string> Parameters
        {
            get
            {
                return parameters;
            }
        }

        public string Nick
        {
            get
            {
                return nick;
            }
        }

        public string Ident
        {
            get
            {
                return ident;
            }
        }

        public string Host
        {
            get
            {
                return host;
            }
        }

        #endregion
    }
}
